package com.hdit.action.admin;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.jsp.PageContext;

import com.hdit.entity.Auction;
import com.hdit.entity.AuctionApply;
import com.hdit.service.AuctionApplyService;

import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.oscache.base.Cache;
import com.opensymphony.oscache.web.ServletCacheAdministrator;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;

/**
 * 前台Action类 - 订单处理
 * ============================================================================
 */

@ParentPackage("admin")
public class AuctionApplyAction extends BaseAdminAction {
	
	private static final long serialVersionUID = 2553137844831167917L;
	private AuctionApply auctionApply;//商品申请

	@Resource
	private AuctionApplyService auctionApplyService;
	
	// 订单信息
	@InputConfig(resultName = "error")
	public String info() {
		
		setResponseNoCache();
		return "info";
	}
	
	// 订单保存
	@Validations(
		requiredStrings = {
			@RequiredStringValidator(fieldName = "auctionApply.auctionNo", message = "专利号必须输入！"),
			@RequiredStringValidator(fieldName = "auctionApply.name", message = "专利名称必须输入！"),
			@RequiredStringValidator(fieldName = "auctionApply.auctionPrice", message = "竞拍底价必须输入！")
		}
	)
	@InputConfig(resultName = "error")
	public String save() {
		auctionApply.setCreateDate(new Date());
		auctionApply.setStatus(0);
		auctionApplyService.save(auctionApply);
		setResponseNoCache();
		return SUCCESS;
	}
	
	// 竞拍申请列表
	public String list() {
		
		
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(AuctionApply.class);
		pager = auctionApplyService.findByPager(pager, detachedCriteria);
		return "list";
	}
	

	// 编辑
	public String edit() {
		auctionApply = auctionApplyService.get(id);
		return INPUT;
	}
	
	@InputConfig(resultName = "error")
	public String update() throws Exception {
		
		try{
		
		AuctionApply persistent = auctionApplyService.load(id);
		
		persistent.setStatus(auctionApply.getStatus());
		
		auctionApplyService.update(persistent);
		
		redirectionUrl = "auction_apply!list.action";
		return SUCCESS;
		
		}
		
		catch(Exception e){
			e.printStackTrace();
			throw e;
		}
	}
	
	// 订单详情
	public String view() {
		return "view";
	}
	
	// 添加
	public String add() {
		return INPUT;
	}
	
	// 更新页面缓存
	private void flushCache() {
		Cache cache = ServletCacheAdministrator.getInstance(getRequest().getSession().getServletContext()).getCache(getRequest(), PageContext.APPLICATION_SCOPE); 
		cache.flushAll(new Date());
	}

	public AuctionApply getAuctionApply() {
		return auctionApply;
	}

	public void setAuctionApply(AuctionApply auctionApply) {
		this.auctionApply = auctionApply;
	}


}